package stw.brassdawn.resource;

import java.util.HashMap;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.GdxRuntimeException;

public class TextureLibrary {
	private static TextureLibrary myLibrary = null;
	private HashMap<String, Texture> myTextures = null;
	
	private TextureLibrary( ) {
		myTextures = new HashMap<String, Texture>();
	}
	
	public static TextureLibrary getTextLibrary( ) {
		if( myLibrary == null ) {
			myLibrary = new TextureLibrary();
		}
		
		return myLibrary;
	}
	
	public void loadTexture( String filename ) {
		if( !myTextures.containsKey(filename) ) {
		
			try {
				Texture texture = new Texture(Gdx.files.internal(filename));
				myTextures.put(filename, texture);
			} catch( GdxRuntimeException gre ) {
				throw new GdxRuntimeException( "In TextureLibrary, loading texture: \n" + gre.getMessage() );
			}
		}
	}
	
	public Texture getTexture( String key ) {
		Texture rv = null;
		if( myTextures.containsKey(key) ) {
			rv = myTextures.get(key);
		} else {
			loadTexture( key );
			rv = myTextures.get(key);
		}
		
		return rv;
	}
}

